<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>BreadthFirstPaths.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="BreadthFirstPaths code in Java">
<meta NAME="TITLE" CONTENT="BreadthFirstPaths code in Java">
<meta NAME="KEYWORDS" CONTENT="BreadthFirstPaths,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>BreadthFirstPaths.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "BreadthFirstPaths.java">BreadthFirstPaths.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac BreadthFirstPaths.java</span>
<span class="comment"> *  Execution:    java BreadthFirstPaths G s</span>
<span class="comment"> *  Dependencies: Graph.java Queue.java Stack.java StdOut.java</span>
<span class="comment"> *  Data files:   </span><span class="url">http://algs4.cs.princeton.edu/41graph/tinyCG.txt</span>
<span class="comment"> *</span>
<span class="comment"> *  Run breadth first search on an undirected graph.</span>
<span class="comment"> *  Runs in O(E + V) time.</span>
<span class="comment"> *</span>
<span class="comment"> *  %  java Graph tinyCG.txt</span>
<span class="comment"> *  6 8</span>
<span class="comment"> *  0: 2 1 5 </span>
<span class="comment"> *  1: 0 2 </span>
<span class="comment"> *  2: 0 1 3 4 </span>
<span class="comment"> *  3: 5 4 2 </span>
<span class="comment"> *  4: 3 2 </span>
<span class="comment"> *  5: 3 0 </span>
<span class="comment"> *</span>
<span class="comment"> *  %  java BreadthFirstPaths tinyCG.txt 0</span>
<span class="comment"> *  0 to 0 (0):  0</span>
<span class="comment"> *  0 to 1 (1):  0-1</span>
<span class="comment"> *  0 to 2 (1):  0-2</span>
<span class="comment"> *  0 to 3 (2):  0-2-3</span>
<span class="comment"> *  0 to 4 (2):  0-2-4</span>
<span class="comment"> *  0 to 5 (1):  0-5</span>
<span class="comment"> *</span>
<span class="comment"> *  %  java BreadthFirstPaths largeG.txt 0</span>
<span class="comment"> *  0 to 0 (0):  0</span>
<span class="comment"> *  0 to 1 (418):  0-932942-474885-82707-879889-971961-...</span>
<span class="comment"> *  0 to 2 (323):  0-460790-53370-594358-780059-287921-...</span>
<span class="comment"> *  0 to 3 (168):  0-713461-75230-953125-568284-350405-...</span>
<span class="comment"> *  0 to 4 (144):  0-460790-53370-310931-440226-380102-...</span>
<span class="comment"> *  0 to 5 (566):  0-932942-474885-82707-879889-971961-...</span>
<span class="comment"> *  0 to 6 (349):  0-932942-474885-82707-879889-971961-...</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>


<span class="comment">/**</span>
<span class="comment"> *  The </span><span class="keyword">&lt;tt&gt;</span><span class="comment">BreadthFirstPaths</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> class represents a data type for finding</span>
<span class="comment"> *  shortest paths (number of edges) from a source vertex </span><span class="keyword">&lt;em&gt;</span><span class="comment">s</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  (or a set of source vertices)</span>
<span class="comment"> *  to every other vertex in an undirected graph.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This implementation uses breadth-first search.</span>
<span class="comment"> *  The constructor takes time proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> + </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment">,</span>
<span class="comment"> *  where </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of vertices and </span><span class="keyword">&lt;em&gt;</span><span class="comment">E</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> is the number of edges.</span>
<span class="comment"> *  It uses extra space (not including the graph) proportional to </span><span class="keyword">&lt;em&gt;</span><span class="comment">V</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For additional documentation, see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://algs4.cs.princeton.edu/41graph"</span><span class="keyword">&gt;</span><span class="comment">Section 4.1</span><span class="keyword">&lt;/a&gt;</span><span class="comment">   </span>
<span class="comment"> *  of </span><span class="keyword">&lt;i&gt;</span><span class="comment">Algorithms, 4th Edition</span><span class="keyword">&lt;/i&gt;</span><span class="comment"> by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">BreadthFirstPaths</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> INFINITY </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="normal">MAX_VALUE</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[]</span><span class="normal"> marked</span><span class="symbol">;</span><span class="normal">  </span><span class="comment">// marked[v] = is there an s-v path</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> edgeTo</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// edgeTo[v] = previous edge on shortest s-v path</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> distTo</span><span class="symbol">;</span><span class="normal">      </span><span class="comment">// distTo[v] = number of edges shortest s-v path</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Computes the shortest path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * and every other vertex in the graph </span><span class="keyword">&lt;tt&gt;</span><span class="comment">G</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> G the graph</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> s the source vertex</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">BreadthFirstPaths</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        marked </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        distTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        edgeTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        </span><span class="function">bfs</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>

<span class="normal">        </span><span class="keyword">assert</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Computes the shortest path between any one of the source vertices in </span><span class="keyword">&lt;tt&gt;</span><span class="comment">sources</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * and every other vertex in graph </span><span class="keyword">&lt;tt&gt;</span><span class="comment">G</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> G the graph</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> sources the source vertices</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="function">BreadthFirstPaths</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Iterable&lt;Integer&gt;</span><span class="normal"> sources</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        marked </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">boolean</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        distTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        edgeTo </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[</span><span class="normal">G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">()];</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> INFINITY</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">bfs</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> sources</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// breadth-first search from a single source</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">bfs</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Queue&lt;Integer&gt;</span><span class="normal"> q </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span>
<span class="normal">            distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> INFINITY</span><span class="symbol">;</span>
<span class="normal">        distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">        marked</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        q</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>

<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">q</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> q</span><span class="symbol">.</span><span class="function">dequeue</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">marked</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">                    distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">                    marked</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">                    q</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// breadth-first search from multiple sources</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">bfs</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">Iterable&lt;Integer&gt;</span><span class="normal"> sources</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Queue&lt;Integer&gt;</span><span class="normal"> q </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Queue</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> s </span><span class="symbol">:</span><span class="normal"> sources</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            marked</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">            distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">            q</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">s</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">q</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> q</span><span class="symbol">.</span><span class="function">dequeue</span><span class="symbol">();</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="normal">marked</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">])</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span>
<span class="normal">                    distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">                    marked</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">                    q</span><span class="symbol">.</span><span class="function">enqueue</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Is there a path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> (or sources) and vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">?</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if there is a path, and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> marked</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the number of edges in a shortest path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span>
<span class="comment">     * (or sources) and vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">?</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the number of edges in a shortest path</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">int</span><span class="normal"> </span><span class="function">distTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">];</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns a shortest path between the source vertex </span><span class="keyword">&lt;tt&gt;</span><span class="comment">s</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> (or sources)</span>
<span class="comment">     * and </span><span class="keyword">&lt;tt&gt;</span><span class="comment">v</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">null</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if no such path.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> v the vertex</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the sequence of vertices on a shortest path, as an Iterable</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="usertype">Iterable&lt;Integer&gt;</span><span class="normal"> </span><span class="function">pathTo</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">Stack&lt;Integer&gt;</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> Stack</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> x</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">=</span><span class="normal"> v</span><span class="symbol">;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">x</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">x</span><span class="symbol">])</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">push</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> path</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// check optimality conditions for single source</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">check</span><span class="symbol">(</span><span class="usertype">Graph</span><span class="normal"> G</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>

<span class="normal">        </span><span class="comment">// check that the distance of s = 0</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distance of source "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> s </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" to itself = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">s</span><span class="symbol">]);</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that for each edge v-w dist[w] &lt;= dist[v] + 1</span>
<span class="normal">        </span><span class="comment">// provided v is reachable from s</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">:</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">adj</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> </span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"-"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"hasPathTo("</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">") = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">));</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"hasPathTo("</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> w </span><span class="symbol">+</span><span class="normal"> </span><span class="string">") = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">));</span>
<span class="normal">                    </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">&gt;</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"-"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distTo["</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"] = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]);</span>
<span class="normal">                    StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distTo["</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> w </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"] = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">                    </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// check that v = edgeTo[w] satisfies distTo[w] + distTo[v] + 1</span>
<span class="normal">        </span><span class="comment">// provided v is reachable from s</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> w </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> w </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> w</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">w</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> w </span><span class="symbol">==</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">continue</span><span class="symbol">;</span>
<span class="normal">            </span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> edgeTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">];</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"shortest path edge "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"-"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> w</span><span class="symbol">);</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distTo["</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> v </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"] = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">v</span><span class="symbol">]);</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"distTo["</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> w </span><span class="symbol">+</span><span class="normal"> </span><span class="string">"] = "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> distTo</span><span class="symbol">[</span><span class="normal">w</span><span class="symbol">]);</span>
<span class="normal">                </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Unit tests the </span><span class="keyword">&lt;tt&gt;</span><span class="comment">BreadthFirstPaths</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> data type.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">In</span><span class="normal"> in </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">In</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="usertype">Graph</span><span class="normal"> G </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Graph</span><span class="symbol">(</span><span class="normal">in</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// StdOut.println(G);</span>

<span class="normal">        </span><span class="type">int</span><span class="normal"> s </span><span class="symbol">=</span><span class="normal"> Integer</span><span class="symbol">.</span><span class="function">parseInt</span><span class="symbol">(</span><span class="normal">args</span><span class="symbol">[</span><span class="number">1</span><span class="symbol">]);</span>
<span class="normal">        </span><span class="usertype">BreadthFirstPaths</span><span class="normal"> bfs </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BreadthFirstPaths</span><span class="symbol">(</span><span class="normal">G</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">);</span>

<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> v </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> v </span><span class="symbol">&lt;</span><span class="normal"> G</span><span class="symbol">.</span><span class="function">V</span><span class="symbol">();</span><span class="normal"> v</span><span class="symbol">++)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">bfs</span><span class="symbol">.</span><span class="function">hasPathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d to %d (%d):  "</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">,</span><span class="normal"> bfs</span><span class="symbol">.</span><span class="function">distTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">));</span>
<span class="normal">                </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> x </span><span class="symbol">:</span><span class="normal"> bfs</span><span class="symbol">.</span><span class="function">pathTo</span><span class="symbol">(</span><span class="normal">v</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                    </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> s</span><span class="symbol">)</span><span class="normal"> StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">                    </span><span class="keyword">else</span><span class="normal">        StdOut</span><span class="symbol">.</span><span class="function">print</span><span class="symbol">(</span><span class="string">"-"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> x</span><span class="symbol">);</span>
<span class="normal">                </span><span class="cbracket">}</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">            </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                StdOut</span><span class="symbol">.</span><span class="function">printf</span><span class="symbol">(</span><span class="string">"%d to %d (-):  not connected</span><span class="specialchar">\n</span><span class="string">"</span><span class="symbol">,</span><span class="normal"> s</span><span class="symbol">,</span><span class="normal"> v</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Mon Mar 21 10:51:08 EDT 2016.
</small></address>

</html>
